package com.chen.blue.converter.system;

import com.chen.blue.domain.dto.system.PostDto;
import com.chen.blue.domain.dto.system.RoleDto;
import com.chen.blue.domain.dto.system.update.PostDtoEdit;
import com.chen.blue.domain.dto.system.update.RoleDtoEdit;
import com.chen.blue.domain.model.system.PostModel;
import com.chen.blue.domain.model.system.RoleModel;
import com.chen.blue.domain.vo.system.PostVo;
import com.chen.blue.domain.vo.system.RoleVo;
import org.springframework.cglib.beans.BeanCopier;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * Created By CLT On 2024/5/10.
 */
@SuppressWarnings("all")
public abstract class PostConverter {

    private static final BeanCopier modelToVo = BeanCopier.create(PostModel.class, PostVo.class, false);
    private static final BeanCopier dtoToModel = BeanCopier.create(PostDto.class, PostModel.class, false);
    private static final BeanCopier dtoEditToModel = BeanCopier.create(PostDtoEdit.class, PostModel.class, false);

    /**
     * model->vo
     *
     * @param model
     * @return
     */
    public static PostVo modelToVo(PostModel model) {
        if (Objects.isNull(model)) {
            return null;
        }
        PostVo postVo = new PostVo();
        modelToVo.copy(model, postVo, null);
        return postVo;
    }

    /**
     * model->volist
     *
     * @param modelList
     * @return
     */
    public static List<PostVo> modelToVoList(List<PostModel> modelList) {
        if (modelList.size() == 0) {
            return null;
        }

        List<PostVo> postVoList = new ArrayList<>();
        for (PostModel postModel : modelList) {
            postVoList.add(modelToVo(postModel));
        }

        return postVoList;

    }

    /**
     * dto->model
     *
     * @param postDto
     * @return
     */
    public static PostModel dtoToModel(PostDto postDto) {

        if (Objects.isNull(postDto)) {
            return null;
        }

        PostModel postModel = new PostModel();
        dtoToModel.copy(postDto, postModel, null);
        return postModel;

    }

    /**
     * editDto->model
     *
     * @param postDtoEdit
     * @return
     */
    public static PostModel dtoEditToModel(PostDtoEdit postDtoEdit) {

        if (Objects.isNull(postDtoEdit)) {
            return null;
        }

        PostModel postModel = new PostModel();
        dtoEditToModel.copy(postDtoEdit, postModel, null);
        return postModel;

    }


}
